package com.sxt.business.controller;

import com.sxt.business.domain.Customer;
import com.sxt.business.service.CustomerService;
import com.sxt.business.vo.CustomerVo;
import com.sxt.system.common.Constant;
import com.sxt.system.common.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: 0812erp
 * @author: 雷哥
 * @create: 2020-01-06 10:43
 **/
@RequestMapping("api/customer")
@RestController
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    /**
     * 查询
     */
    @RequestMapping("loadAllCustomer")
    public Object loadAllCustomer(CustomerVo customerVo){
        return this.customerService.queryAllCustomer(customerVo);
    }

    /**
     * 添加
     */
    @RequestMapping("addCustomer")
    public ResultObj addCustomer(Customer customer){
        try {
            customer.setAvailable(Constant.AVAILABLE_TRUE);
            this.customerService.saveCustomer(customer);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e)
        {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }

    }

    /**
     * 修改
     */
    @RequestMapping("updateCustomer")
    public ResultObj updateCustomer(Customer customer){
        try {
            this.customerService.updateCustomer(customer);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e)
        {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }

    }

    /**
     * 删除
     */
    @RequestMapping("deleteCustomer")
    public ResultObj deleteCustomer(Integer id){
        try {
            this.customerService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e)
        {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     */
    @RequestMapping("batchDeleteCustomer")
    public ResultObj batchdeleteCustomer(Integer[] ids){
        try {
            List<Integer> idsList=new ArrayList<>();
            for (Integer id : ids) {
                idsList.add(id);
            }
            this.customerService.removeByIds(idsList);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e)
        {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 查询所有可用的客户
     */
    @GetMapping("getAllAvailableCustomer")
    public Object getAllAvailableCustomer(){
        return this.customerService.getAllAvailableCustomer();
    }


}
